package gui;

import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.filechooser.FileFilter;

import odl.Controller;

public class Gui extends JFrame {

	public static Gui getGUI() {
		return gui == null ? new Gui() : gui;
	}
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Gui ex = new Gui();
				ex.setVisible(true);
			}
		});
	}
	JButton openButton;
	JButton exportButton;
	private ListPanel interfaces;
	private MainPanel main;
	static Gui gui;

	final JFileChooser fc = new JFileChooser(".");

	Controller controller;

	public Gui() {
		initUI();
		gui = this;
		controller = Controller.getController();
	}

	public final void initUI() {

		JToolBar toolbar = new JToolBar();
		try {

			ImageIcon icon = new ImageIcon(getClass().getResource("open.png"));
			ImageIcon iconCSV = new ImageIcon(getClass().getResource("csv_icon.png"));
			openButton = new JButton(icon);
			exportButton = new JButton(iconCSV);
		} catch (Exception e) {
			openButton = new JButton();
			exportButton = new JButton();
		}

		openButton.setText("  Open ODL  ");
		exportButton.setText("  Export CSV  ");
		toolbar.add(openButton);
		toolbar.add(exportButton);
		JLabel title = new JLabel("  Thibaud Guilpain & Germain Potet  ");
		toolbar.add(title,BorderLayout.EAST);
		openButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				
				int returnVal = fc.showOpenDialog(Gui.this);

				if (returnVal == JFileChooser.APPROVE_OPTION) {
					
					try {
						Controller.getController().open(fc.getSelectedFile());
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 
				} else {
					System.out.println("Open command cancelled by user.");
				}
			}

		});
		exportButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser fc = new JFileChooser(".");
				ExampleFileFilter filter = new ExampleFileFilter();
			    filter.addExtension("csv");
				int returnVal = fc.showSaveDialog(Gui.this);

				if (returnVal == JFileChooser.APPROVE_OPTION) {
					
					try {
						Controller.getController().get_Schema().generateCSV(fc.getSelectedFile());
					} catch (Exception e) {
						e.printStackTrace();
					} 
				} else {
					System.out.println("export command cancelled by user.");
				}
			}
			
		});
		add(toolbar, BorderLayout.NORTH);

		setTitle("IFT3913");
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		interfaces = new ListPanel(new JList(new String[] {}), "Interfaces");
		add(interfaces, BorderLayout.WEST);
		main = new MainPanel();
		this.setSize(500, 500);
		add(main);

		// this.pack();
		ListSelectionListener l = new ListSelectionListener() {

			@Override
			public void valueChanged(ListSelectionEvent e) {

				String i_name = (String) interfaces.getSelectedValue();
				main.attributes.update(controller
						.getInterfaceAttributeNames(i_name));
				main.operations.update(controller
						.getInterfaceOperationNames(i_name));
				main.relations.update(controller
						.getInterfaceRelationNames(i_name));
				main.details.update(controller.getDetails(controller
						.getCurrent_Interface()));
				main.metrics.update((controller
						.getCurrent_Interface().getMetricsStrings()));
			}
		};
		interfaces.addListSelectionListener(l);
	}


	class CSVFilter extends FileFilter
	{

		@Override
		public boolean accept(File f) {
			
			return f.getAbsolutePath().endsWith(".csv");
		}

		@Override
		public String getDescription() {
			// TODO Auto-generated method stub
			return "CSV files";
		}
		
	}

	public void updateInterfaces(Vector<String> _data) {
		interfaces.update(_data);
	}
}